﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BUS
{
    public class CInputKeyRunningAuto
    {

        public void Install(ref RichTextBox richTextBox)
        {
            richTextBox.KeyDown+=new KeyEventHandler(KeyDown);
            richTextBox.KeyUp+=new KeyEventHandler(KeyUp);
            richTextBox.KeyPress+=new KeyPressEventHandler(KeyPress);
        }

        public void Uninstall(ref RichTextBox richTextBox)
        {
            richTextBox.KeyDown -= new KeyEventHandler(KeyDown);
            richTextBox.KeyUp -= new KeyEventHandler(KeyUp);
            richTextBox.KeyPress -= new KeyPressEventHandler(KeyPress);
        }

        private Keys _keyold;
        private DTO.CListKeyCode _ListKeyCode = new DTO.CListKeyCode();

        public void KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;

            if (e.KeyCode != _keyold)
            {
                string _KeyName = CTransferKeysCode.SwithText(e.KeyValue, _ListKeyCode);
                RichTextBox _rtxt = (RichTextBox)sender;
                int _start = _rtxt.SelectionStart;
                int _length = _rtxt.SelectionLength;

                _KeyName = "<" + _KeyName + ">";

                for (int i = _start - 1; i >= 0; i--)
                {
                    if (_rtxt.Text[i] == '>' || _rtxt.Text[i] == ')')
                        break;
                    else if (_rtxt.Text[i] == '<' || _rtxt.Text[i] == '(')
                    {
                        _rtxt.SelectionStart = i;
                        break;
                    }
                }

                _rtxt.Text = _rtxt.Text.Insert(_rtxt.SelectionStart, _KeyName);
                _rtxt.SelectionStart = _start + _KeyName.Length;
                _rtxt.SelectionLength = _length;
                _keyold = e.KeyCode;
                //BUS.CCorlorRichTextBox.Highlight(_rtxt);
            }
        }

        public void KeyUp(object sender, KeyEventArgs e)
        {
            e.Handled = true;

            string _KeyName = CTransferKeysCode.SwithText(e.KeyValue, _ListKeyCode);
            RichTextBox _rtxt = (RichTextBox)sender;
            int _start = _rtxt.SelectionStart;
            int _length = _rtxt.SelectionLength;
            bool _DaAdd = false;
            if(_keyold==e.KeyCode)
                _keyold = 0;

            if (_start > 0)
            {
                if (_length == 0)
                {
                    if (_rtxt.Text[_start - 1] == '>')
                    {
                        for (int i = _start - 2; i >= 0; i--)
                        {
                            if (_rtxt.Text[i] == '<')
                            {
                                _rtxt.SelectionStart = i + 1;
                                _rtxt.SelectionLength = _start - i - 2;
                                break;
                            }
                        }
                        if (_rtxt.SelectedText == _KeyName)
                        {
                            if (_KeyName.Length > 1)
                            {
                                _KeyName = "(" + _KeyName + ")";
                            }
                            int _tempstart = _rtxt.SelectionStart - 1;
                            _rtxt.Text = _rtxt.Text.Remove(_tempstart, _rtxt.SelectionLength + 2);
                            _rtxt.Text = _rtxt.Text.Insert(_tempstart, _KeyName);
                            _rtxt.SelectionStart = _tempstart + _KeyName.Length;
                            _DaAdd = true;
                        }
                        else
                        {
                            _rtxt.SelectionStart = _start;
                            _rtxt.SelectionLength = _length;
                        }
                    }
                }
                if(!_DaAdd)
                {
                    for (int i = _start + _length; i < _rtxt.Text.Length; i++)
                    {
                        if (_rtxt.Text[i] == '<' || _rtxt.Text[i] == '(')
                            break;
                        if (_rtxt.Text[i] == '>' || _rtxt.Text[i] == ')')
                        {
                            _rtxt.SelectionStart = i + 1;
                            break;
                        }
                    }
                    _KeyName = "</" + _KeyName + ">";

                    if (_start == _rtxt.SelectionStart)
                    {
                        _rtxt.Text = _rtxt.Text.Insert(_rtxt.SelectionStart + _length, _KeyName);
                        _rtxt.SelectionStart = _start + _KeyName.Length+_length;
                    }
                    else
                    {
                        _rtxt.Text = _rtxt.Text.Insert(_rtxt.SelectionStart, _KeyName);
                        _rtxt.SelectionStart = _start;
                        _rtxt.SelectionLength = _length;
                    }
                }
            }
            //BUS.CCorlorRichTextBox.Highlight(_rtxt);
        }

        public void KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

    }
}
